/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LucasCode;
using System.IO;

namespace quickReminder
{
    public partial class FormCreateArchive : Form
    {
        MatchArchive m_Archive;
        public FormCreateArchive(MatchArchive a)
        {
            m_Archive = a;
            InitializeComponent();
        }

        private void Zipper_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            m_Archive.CreateArchive(
                                    Path.Combine(Properties.Settings.Default.ArchivePath,
                                                    m_Archive.GetReplacedString(Properties.Settings.Default.ArchivePathTemplate)
                                                 ) + ".qma"
                                    );
            MatchFile[] files =  m_Archive.Files;
            int total = 0, current=0;
            foreach (MatchFile f in files)
                if (f.AddToArchive)
                    total++;
            foreach(MatchFile f in files)
            {
                if (worker.CancellationPending)
                    break;
                Zipper.ReportProgress(GetProgress(current, total), f);
                if (f.AddToArchive)
                    m_Archive.FileProcess(f);
                Zipper.ReportProgress(GetProgress(++current, total));
            }
        }

        private int GetProgress(int current, int maximum)
        {
            if (maximum != 0)
                return GetProgress(((double)current / maximum) * 100);
            else
                return GetProgress(0);
        }

        private int GetProgress(double progress)
        {
            if (progress > 100)
                return 100;
            else if (progress < 0)
                return 0;
            else
                return (int)Math.Round(progress);
        }

        private void Zipper_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState != null)
                lFilename.Text = Path.GetFileName(((MatchFile)e.UserState).Filename);
            else
                lFilename.Text = String.Empty;
            pbOverallProgress.Value = e.ProgressPercentage;
        }

        private void FormCreateArchive_Shown(object sender, EventArgs e)
        {
            Zipper.RunWorkerAsync();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Zipper.CancelAsync();
            button1.Enabled = false;
        }

        private void Zipper_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void FormCreateArchive_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Zipper.IsBusy)
                Zipper.CancelAsync();
        }
    }
}